/* Sample JavaScript file added with ScriptTag resource.
This sample file is meant to teach best practices.
Your app will load jQuery if it's not defined.
Your app will load jQuery if jQuery is defined but is too old, e.g. < 1.7.
Your app does not change the definition of $ or jQuery outside the app.
Example: if a Shopify theme uses jQuery 1.4.2, both of these statements run in the console will still return '1.4.2'
once the app is installed, even if the app uses jQuery 1.9.1:
jQuery.fn.jquery => "1.4.2"
$.fn.jquery -> "1.4.2"
*/
/* Using a self-executing anonymous function - (function(){})(); - so that all variables and functions defined within
aren’t available to the outside world. */
(function () {
/* Load Script function we may need to load jQuery from the Google's CDN */
/* That code is world-reknown. */
/* One source: http://snipplr.com/view/18756/loadscript/ */
var head = document.head;
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = 'https://app.facturama.mx/Public/css/storeFrontStyles.css';
head.appendChild(link);
var loadScript = function (url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
// If the browser is Internet Explorer.
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
// For any other browser.
} else {
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
};
/* This is my app's JavaScript */
var myAppJavaScript = function ($) {
var html = '
' +
'
' +
'
' +
' ' +
'
' +
'' +
'';
if (Shopify != null && Shopify.checkout != null && Shopify.checkout != undefined) {
Shopify.Checkout.OrderStatus.addContentBox(html);
$("#containerStatus").parent().hide();
//showFormInvoice($);
loadControlsClients($);
} else if (Shopify != null && Shopify.shop != null && Shopify.shop != undefined && $("#facturama").is(':visible')) {
var head = document.head;
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = 'https://app.facturama.mx/Public/css/storeFrontStylesV2.css';
head.appendChild(link);
$("#facturama").append(html);
$("#containerStatus").parent().hide();
loadBilling($);
}
};
function getHost() {
const scripts = document.getElementsByTagName('script');
let scriptUrl = '';
for (let i = 0; i < scripts.length; i++) {
if (scripts[i].src.includes('shopify/loadScript.js')) {
scriptUrl = scripts[i].src;
break;
}
}
return new URL(scriptUrl).origin;
}
function loadBilling($) {
var host = getHost();
$("#containerStatus").parent().show();
$("#status").show();
$.ajax({
url: host + '/Shopify/Clients/Billing',
type: 'POST',
dataType: 'jsonp',
//data: { checkout: model, address: address },
success: function (result) {
$("#containerFacturama").empty().append(result.partial);
$("#status").hide();
},
error: function () {
$("#containerErrorFacturama").empty().append("Ocurrió un error al procesar la petición.");
$("#status").hide();
},
});
}
function loadControlsClients($) {
var host = getHost();
$("#containerStatus").parent().show();
$("#status").show();
var model = btoa(JSON.stringify(getCheckOutElements()));
var address = btoa(JSON.stringify(getAddressElements()));
$.ajax({
url: host + '/Shopify/Clients/GetClientStorePartial',
type: 'POST',
dataType: 'jsonp',
data: { checkout: model, address: address },
success: function (result) {
if (result.success == true) {
//agregar el contenedor del Formulario de Datos Fiscales
var htmlLimit = result.showLimit == false
? "Terminó el tiempo en el que puedes generar tu factura, ponte en contacto con el vendedor."
: result.limit == null ? "" : result.limit == 9999
? "Tienes el resto del mes para generar la factura"
: "Tienes " + result.limit + " dias para generar tu factura (aplica desde que se crea la orden de compra).";
var html = '' +
'
' +
'Factura tu pedido | ' +
' | ' +
'
' +
'' +
'
' +
'' +
'
Proporciona la siguiente información para generar la Factura con base a tu compra. ' +
'Esta factura será enviada al correo que proporciones en cuanto se acredite el pago. ' +
'
' +
'La version del CFDI es ' + result.version +'.
' +
'' + htmlLimit + '' +
'
' +
'
Nota: si no carga el formulario, favor de refrescar la pagina.
';
//'+ result.version +'
$("#containerFacturama").empty().append(html);
$("#containerDataClient").empty().append(result.partial);
if ($("#Rfc").val() != undefined && $("#Rfc").val() != "")
$("#Rfc").trigger('onkeyup');
var state = $("#State").val();
var stateFacturama = state.toUpperCase() == "MICHOACAN"
? "MICHOACAN DE OCAMPO"
: state.toUpperCase() == "VERACRUZ"
? "VERACRUZ DE IGNACIO DE LA LLAVE" : state.toUpperCase();
$("#cmbState").val(stateFacturama);
var use = $('[name="CfdiUse"]').val();
$('#cfdiUse').val(use);
$("#useIva").on("change", function () {
if ($("#useIva").val() == 8)
$("#labelNote").show();
else
$("#labelNote").hide();
});
$("#version").val(result.version);
} else {
$("#containerStatus").parent().hide();
}
$("#status").hide();
},
error: function () {
$("#containerErrorFacturama").empty().append("Ocurrió un error al procesar la petición.");
//alert("Ocurrio un error al procesar la peticion.");
$("#status").hide();
},
});
}
function getCheckOutElements() {
var model = new Object();
var address = new Object();
if (Shopify != null && Shopify.checkout != null) {
model.BillingAddress = address;
model.CreatedAt = Shopify.checkout.created_at;
model.Currency = Shopify.checkout.currency;
if (Shopify.checkout.source_name != "pos") {
model.customer_id = Shopify.checkout.customer_id != null ? Shopify.checkout.customer_id.toString() : "";
model.Email = Shopify.checkout.email;
}
model.order_id = Shopify.checkout.order_id.toString();
model.Token = Shopify.checkout.token;
model.Shop = Shopify.shop;
}
return model;
}
function getAddressElements() {
var address = new Object();
if (Shopify != null && Shopify.checkout != null && Shopify.checkout.billing_address != null) {
address.Address1 = Shopify.checkout.billing_address.address1;
//address.Address2 = Shopify.checkout.billing_address.address2.replace(/\“/g, "").replace(/\”/g, "");
address.City = Shopify.checkout.billing_address.city;
address.Company = Shopify.checkout.billing_address.company;
address.Country = Shopify.checkout.billing_address.country;
address.CountryCode = Shopify.checkout.billing_address.country_code;
address.FirstName = Shopify.checkout.billing_address.first_name;
address.LastName = Shopify.checkout.billing_address.last_name;
address.Phone = Shopify.checkout.billing_address.phone;
address.Province = Shopify.checkout.billing_address.province;
address.ProvinceCode = Shopify.checkout.billing_address.province_code;
address.Zip = Shopify.checkout.billing_address.zip;
}
return address;
}
/* If jQuery has not yet been loaded or if it has but it's too old for our needs,
we will load jQuery from the Google CDN, and when it's fully loaded, we will run
our app's JavaScript. Set your own limits here, the sample's code below uses 1.9.1
as the minimum version we are ready to use, and if the jQuery is older, we load 1.9.1 */
if ((typeof jQuery === 'undefined') || (parseInt(jQuery.fn.jquery) === 1 && parseFloat(jQuery.fn.jquery.replace(/^1\./, "")) < 9.1)) {
loadScript('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', function () {
jQuery191 = jQuery.noConflict(true);
myAppJavaScript(jQuery191);
});
} else {
myAppJavaScript(jQuery);
}
})();